﻿/*端子连线飞线*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyLine : MonoBehaviour
{
    // Use this for Globalialization
    //Vector3 pos;
    Vector3 pos1;
    Vector3 pos2;
    Vector3 scale0;
    float objectHeight = 2.0f;
    public AssemblyRun assemblyRun;

    void Start()
    {
        scale0 = transform.localScale;
        assemblyRun= GameObject.Find("AssemblyRun").GetComponent<AssemblyRun>();
    }


    // Update is called once per frame
    void Update()
    {
        if (assemblyRun.isFly == 0)
        {
            //flyline.transform.position = new Vector3(0, 0, 0);
            //flyline.gameObject.SetActive(false);
        }
        else
        {
            if (assemblyRun.isFly == 1)
            {
                //pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
                // pos1 = Camera.main.ScreenToWorldPoint(pos1);  
                pos1 = assemblyRun.StartFly;
                pos2 = pos1;
                assemblyRun.isFly = 2;

            }

            else if (assemblyRun.isFly == 2)
            {
                pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
                pos2 = Camera.main.ScreenToWorldPoint(pos2);
            }
            //Debug.Log("pos1:" + pos1);
            //Debug.Log("pos2:" + pos2);
            if (pos2 != pos1)
            {
                Vector3 v3 = pos2 - pos1;
                transform.position = pos1 + (v3) / 2.0f;
                //transform.localScale.y= v3.magnitude / objectHeight;
                transform.localScale = new Vector3(0.0002f, v3.magnitude / objectHeight, 0.0002f);
                transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
            }

            if (Input.GetMouseButtonDown(1))//右键取消飞线
            {
                pos1 = new Vector3(0f, 0f, 0f);
                pos2 = pos1;
                assemblyRun.StartPointName = "";
                assemblyRun.EndPointName = "";
                transform.localScale = scale0;
                transform.gameObject.SetActive(false);
                //Global.isFly = 0;

            }
        }

    }
}

